package com.bjpower.settings;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.bjpower.domain.DicPayType;
import com.bjpower.domain.DicPayWay;
import com.bjpower.mapper.DicPayTypeMapper;
import com.bjpower.mapper.DicPayWayMapper;
import com.bjpower.models.vo.PayWayDicVo;
import com.bjpower.utlis.RedisHelper;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义配置类以及自定义Bean
 * 实现CommandLineRunner接口，创建缓存到内存中
 */
@Configuration
public class BossConfiguration implements CommandLineRunner {

    @Resource
    private ApplicationContext applicationContext;

    public static List<PayWayDicVo> dicPayWayVoList = new ArrayList<>();

    public static HashMap<String,List<PayWayDicVo>>  dicPayWayMap = new HashMap<>();

    public static Map<String,String> DIC_PAY_TYPE_MAP =  new HashMap<>();


    public static Map<String,String> DIC_PAY_WAY_MAP = new HashMap<>();
    @Bean
    public RedisHelper redisHelper(StringRedisTemplate stringRedisTemplate){
        return new RedisHelper(stringRedisTemplate);
    }

    /**
     * 添加分页插件配置
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        return interceptor;
    }

    @Override
    public void run(String... args) throws Exception {
        //查询数据库，将数据缓存到内存中，通过spring上下文对象拿到Mapper类
        DicPayWayMapper bean = applicationContext.getBean(DicPayWayMapper.class);
        QueryWrapper<DicPayWay> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("pay_way_code");
        List<DicPayWay> dicPayWayList = bean.selectList(wrapper);
        //将查询到的数据转换为list<PayWayDicVo>
        for (DicPayWay dicPayWay : dicPayWayList) {
            PayWayDicVo payWayDicVo = new PayWayDicVo();
            payWayDicVo.setCode(dicPayWay.getPayWayCode());
            payWayDicVo.setName(dicPayWay.getPayWayName());
            dicPayWayVoList.add(payWayDicVo);
            DIC_PAY_WAY_MAP.put(dicPayWay.getPayWayCode(),dicPayWay.getPayWayName());
        }
        //查询出所有的支付类型
        DicPayTypeMapper payTypeMapper = applicationContext.getBean(DicPayTypeMapper.class);
        QueryWrapper<DicPayType> payTypeWrapper = new QueryWrapper<>();
        payTypeWrapper.orderByAsc("pay_type_code");
        List<DicPayType> dicPayTypeList = payTypeMapper.selectList(payTypeWrapper);

        //将相同的支付类型封装到放入payTypeList<PayWayDicVo>中再将payTypeList<PayWayDicVo>放入map中 key为支付类型code
        String payWayCode = "";
        for (DicPayType dicPayType : dicPayTypeList) {
            payWayCode = dicPayType.getPayWayCode();
            if (dicPayWayMap.containsKey(payWayCode)) {
                PayWayDicVo payWayDicVo = new PayWayDicVo();
                payWayDicVo.setCode(dicPayType.getPayTypeCode());
                payWayDicVo.setName(dicPayType.getPayTypeName());
                dicPayWayMap.get(payWayCode).add(payWayDicVo);
            } else {
                List<PayWayDicVo> itemVoList = new ArrayList<>();
                PayWayDicVo payWayDicVo = new PayWayDicVo();
                payWayDicVo.setCode(dicPayType.getPayTypeCode());
                payWayDicVo.setName(dicPayType.getPayTypeName());
                itemVoList.add(payWayDicVo);
                dicPayWayMap.put(payWayCode,itemVoList);
            }
            DIC_PAY_TYPE_MAP.put(dicPayType.getPayTypeCode(),dicPayType.getPayTypeName());
        }
    }
}
